package com.example.controller;

import com.example.common.HttpStatusConstants;
import com.example.common.R;
import com.example.model.req.AddServiceReq;
import com.example.model.resp.HomeListServiceResp;
import com.example.model.resp.ListServiceResp;
import com.example.service.ServiceService;
import com.example.utils.ImageUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@ResponseBody
@RestController
@RequestMapping("/service")
public class ServiceController {

    @Autowired
    private ServiceService serviceService;
    @Autowired
    private ImageUtil imageUtil;

    // 管理员添加服务
    @PostMapping("/admin/addService")
    public R<String> addService(@RequestParam("pic") MultipartFile pic,
                                @RequestParam("serviceName") String serviceName, @RequestParam("categoryId") Integer categoryId) throws IOException {
        if (pic == null || serviceName == null || categoryId == null) {
            return R.error(HttpStatusConstants.SERVICE_UNAVAILABLE, "");
        }
        String imgName = imageUtil.getRandomImgName(pic.getOriginalFilename());
        FileInputStream inputStream = null;
        if (!pic.isEmpty()) {
            inputStream = (FileInputStream) pic.getInputStream();
        }
        if (inputStream == null){
            return R.error(HttpStatusConstants.SERVICE_UNAVAILABLE, "");
        }
        AddServiceReq addServiceReq = AddServiceReq.builder()
                .categoryId(categoryId)
                .pic(imageUtil.uploadImageQiniu(inputStream, imgName))
                .serviceName(serviceName)
                .build();
        return serviceService.addService(addServiceReq);
    }

    @GetMapping("/listService/{categoryId}")
    public R<List<ListServiceResp>> getServiceListByCategory(@PathVariable("categoryId") Integer categoryId) {
        return serviceService.listServiceByCategory(categoryId);
    }

    // 删除服务
    @DeleteMapping("/admin/delete/{id}")
    public R<String> deleteService(@PathVariable Integer id) {
        if (id == null) {
            return R.error(HttpStatusConstants.SERVICE_UNAVAILABLE, "");
        }
        return serviceService.deleteService(id);
    }

    // 首页按分类展示的前几条服务
    @GetMapping("/homeListServiceAllCategory")
    public R<List<HomeListServiceResp>> getHomeListServiceAllCategory() {
        return serviceService.getHomeListServiceAllCategory();
    }
}
